﻿using iTextSharp.text;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms.DataVisualization.Charting;

namespace ITextSharpPDF
{
    class PrintPDF
    {
        string imgPath = AppDomain.CurrentDomain.BaseDirectory + "\\logo.jpg";
        string PDFFilePath = string.Format("{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss"));
        PDFGenerator PDF = null;

        public void Test()
        {
            PDF = new PDFGenerator(PDFFilePath);

            //创建页眉+图片+下划线
            PDF.SetPageHeader(imgPath, "12312da", "5433241", 20, true);

            PDF.Open();

            PDF.SetTable(CreateDemoData(), 20,16);

            //写入元数据
            //PDF.SetMateData();

            PDF.SetImage(imgPath,300);
            //中间内容
            PDF.SetContent(16, "Hello World", "标题字体：我是中国人/I'm Chinease!", "标题字体：我是中国人/I'm Chinease!");
            PDF.SetContent("Hello World", "标题字体：我是中国人/I'm Chinease!", "标题字体：我是中国人/I'm Chinease!");
            PDF.SetImage(imgPath);
            PDF.SetContent(26, "Hello World", "标题字体：我是中国人/I'm Chinease!", "标题字体：我是中国人/I'm Chinease!");

            PDF.Close();//关闭文档

            //设置水印
            PDF.SetWaterMark(PDFFilePath, imgPath, 50);
        }

        private DataTable CreateDemoData()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("ID"));
            dataTable.Columns.Add(new DataColumn("Name"));
            dataTable.Columns.Add(new DataColumn("Sex"));
            dataTable.Columns.Add(new DataColumn("Age"));
            dataTable.Columns.Add(new DataColumn("Phone"));
            dataTable.Columns.Add(new DataColumn("Address"));

            for (int i = 0; i < 20; i++)
            {
                dataTable.Rows.Add(i, $"第{i}行", i * new Random().Next(10));
            }

            return dataTable;
        }
    }
}